package com.yc.tomcat.core;

import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ParseWebXml {
	private static Map<String, String> map = new Hashtable<String, String>();

	public ParseWebXml() {
		init();
	}

	@SuppressWarnings("unchecked")
	private void init() {
		SAXReader reader = new SAXReader();
		Document doc = null;
		
		try {
			doc = reader.read(this.getClass().getClassLoader().getResourceAsStream("web.xml"));
			List<Element> mines = doc.selectNodes("//mime-mapping");
			for (Element el : mines) {
				map.put(el.selectSingleNode("extension").getText(), el.selectSingleNode("mime-type").getText());
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 根据扩展名获取类型
	 * @param extension
	 * @return
	 */
	public static String getContentType(String extension) {
		return map.get(extension);
	}
	
}
